﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.storeSerialNum
{
    public partial class Edit : EditPagebase<ISerialNumContract, SerialNum>
    {
        public IInStoreTypeContract InStoreTypeContract { get; set; }
        public IOutStoreTypeContract OutStoreTypeContract { get; set; }
        public IWarehouseContract WarehouseContract { get; set; }
        public IProductContract ProductContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<InStoreType>(InStoreTypeContract.GetResolvedTree(), 0L, ed_InStoreType,true);
                //FineUIListHelper.BindDDL(InStoreTypeContract.EntityName, this.InStoreTypeContract.GetSelectList(), 0L, ed_InStoreType,true);
                //FineUIListHelper.BindDDL<OutStoreType>(OutStoreTypeContract.GetResolvedTree(), 0L, ed_OutStoreType);
                //FineUIListHelper.BindDDL(OutStoreTypeContract.EntityName, this.OutStoreTypeContract.GetSelectList(), 0L, ed_OutStoreType);
                //FineUIListHelper.BindDDL<Warehouse>(WarehouseContract.GetResolvedTree(), 0L, ed_Warehouse,true);
                //FineUIListHelper.BindDDL(WarehouseContract.EntityName, this.WarehouseContract.GetSelectList(), 0L, ed_Warehouse,true);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(SerialNum entity)
        {
            ed_Name.SetValue(entity.Name);
            if (!entity.InStoreTypeId.IsNull())
                ed_InStoreType.SetValue(entity.InStoreTypeId);
            ed_InStoreId.SetValue(entity.InStoreId);
            if (!entity.OutStoreTypeId.IsNull())
                ed_OutStoreType.SetValue(entity.OutStoreTypeId);
            ed_OutStoreId.SetValue(entity.OutStoreId);
            if (!entity.WarehouseId.IsNull())
                ed_Warehouse.SetValue(entity.WarehouseId);
            ed_SerialNumber.SetValue(entity.SerialNumber);
            ed_BirthDay.SetValue(entity.BirthDay);
            ed_Batch.SetValue(entity.Batch);
        }

        protected override OperationResult SaveData(SerialNum entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            if (ed_InStoreType.GetValue().ToLong() == 0L)
                entity.InStoreTypeId = 0L;
            else
                entity.InStoreTypeId = ed_InStoreType.GetValue().ToLong();
            entity.InStoreId = ed_InStoreId.GetValue().ToLong();
            if (ed_OutStoreType.GetValue().ToLong() == 0L)
                entity.OutStoreTypeId = null;
            else
                entity.OutStoreTypeId = ed_OutStoreType.GetValue().ToLong();
            entity.OutStoreId = ed_OutStoreId.GetValue().ToLong();
            if (ed_Warehouse.GetValue().ToLong() == 0L)
                entity.WarehouseId = 0L;
            else
                entity.WarehouseId = ed_Warehouse.GetValue().ToLong();
            entity.SerialNumber = ed_SerialNumber.GetValue().ToString();
            entity.BirthDay = ed_BirthDay.GetValue().ToNullableDateTime();
            if(entity.BirthDay ==DateTime.MinValue)
                entity.BirthDay = null;
            entity.Batch = ed_Batch.GetValue().ToString();
            return base.SaveData(entity);
            
        }
    }
}
